package com.ithaipeng.redanttakeaway.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ithaipeng.redanttakeaway.exception.CustomException;
import com.ithaipeng.redanttakeaway.pojo.ShoppingCart;
import com.ithaipeng.redanttakeaway.service.IShoppingCartService;
import com.ithaipeng.redanttakeaway.tools.BaseContext;
import com.ithaipeng.redanttakeaway.tools.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody ShoppingCart shoppingCart){
        boolean isSuccess;
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //添加userId 一个用户可以有多个购物车，但一个购物车只对应一个dishId
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getUserId()!=null,ShoppingCart::getUserId,shoppingCart.getUserId());
        if(shoppingCart.getDishId()!=null){
            //说明该购物车是菜品，而非套餐
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            //说明该购物车是套餐，而非是菜品
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //进行查询
        ShoppingCart isNullshoppingCart = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if (isNullshoppingCart==null){
            //说明是第一次添加，那么
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //添加到数据库
             isSuccess = shoppingCartService.save(shoppingCart);
        }else {
            //说明数据库有对应的购物车，则说明是添加数量的,或者更改什么
            isNullshoppingCart.setNumber(isNullshoppingCart.getNumber()+1);
            //此时根据id修改就行
             isSuccess = shoppingCartService.updateById(isNullshoppingCart);
        }
        if (isSuccess){
            return Result.success("添加购物车成功!");
        }else {
            return Result.error("添加购物车失败!");
        }
    }

    /**
     * 展示购物车!
     * @param shoppingCart
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(ShoppingCart shoppingCart){
        //添加用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper=new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getUserId()!=null,ShoppingCart::getUserId,shoppingCart.getUserId());
        shoppingCartLambdaQueryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        //查询
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        if(shoppingCartList.isEmpty()){
            throw new CustomException("购物车空荡荡的，快去添加吧!");
        }else {
            return Result.success(shoppingCartList);
        }
    }
    /**
     * 清空购物车!
     * @return
     */
    @DeleteMapping("/clean")
    public Result<String> clean(){
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper=new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        shoppingCartLambdaQueryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
        boolean isSuccess = shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        if (isSuccess){
            return Result.success("清空成功!");
        }else {
            throw new CustomException("很抱歉服务器出现了异常,请联系商家!");
        }
    }

    /**
     * 减少购物车里相同订单的相同物品的数量！
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public Result<String> sub(@RequestBody ShoppingCart shoppingCart){
        boolean isSuccess;
        //添加用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper=new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getUserId()!=null,ShoppingCart::getUserId,shoppingCart.getUserId());
        if (shoppingCart.getDishId()!=null){
            //说明是菜品!
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            //说明是套餐!
            shoppingCartLambdaQueryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //进行等值查询。有且仅有一个!
        ShoppingCart cart = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if(cart.getNumber()!=1){
            cart.setNumber(cart.getNumber()-1);
           isSuccess= shoppingCartService.updateById(cart);
        }else {
            //此时数据库对应的数量为1 再减就是删除
           isSuccess= shoppingCartService.removeById(cart);
        }
        if (isSuccess){
            return Result.success("操作成功!");
        }else {
            throw new CustomException("很抱歉服务器出现错误,请联系商家!");
        }
    }
}
